package com.fjzzct.report.dao.Impl;

import com.fjzzct.report.dao.TicketCheckDao;
import com.fjzzct.report.domain.ComicCheck;
import com.fjzzct.report.domain.PagerEntity;
import com.fjzzct.report.utils.LogHelper;
import com.fjzzct.report.utils.PaginationHelper;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by cwb on 2015/10/29.
 */
@Repository
public class TicketCheckDaoImpl extends BaseDaoImpl implements TicketCheckDao  {

    @Override
    public PagerEntity GetTestList(int page,int rows) {

        try {
            PagerEntity pe=new PagerEntity();
            String sqlTtal="select count(*) ct"+
                    " from ComicCheck pj  " +
                    " where pj.DeleteMark=0 ";

            Map<String, ?> totalmap=find(sqlTtal,null);
            if(totalmap!=null&&totalmap.size()>0){
                pe.setTotal(Integer.parseInt(String.valueOf(totalmap.get("ct"))));
            }

            String sqlString = "select * from ComicCheck where DeleteMark=0 ";
            sqlString=PaginationHelper.BuildPaginationSql(sqlString,"CreateTime desc",page,rows);
            HashMap<String, Object> args = new HashMap<String, Object>();
            List<ComicCheck> lck=getObjList(sqlString,ComicCheck.class,args);
            if(lck!=null)
            pe.setRows(lck);
            return pe;
        }
        catch (DataAccessException ex)
        {
            LogHelper.WriteErrorLog(ex);
            return null;
        }
    }

    @Override
    public boolean Add(ComicCheck comicCheckm) {
        try {
            String sqlString = "insert into ComicCheck(NO,TicketDate,CreateIP)"
                    +" values(:NO,:TicketDate,:CreateIP)";
            saveOrUpdateObject(sqlString, comicCheckm);
            return true;
        }
        catch (DataAccessException ex)
        {
            LogHelper.WriteErrorLog(ex);
            return false;
        }
    }

    @Override
    public boolean Edit(ComicCheck comicCheckm) {
        try {
            String sqlString = "update ComicCheck set NO=:NO,TicketDate=:TicketDate,"
                    +"ModifyTime=:ModifyTime"
                    +" where ID=:ID ";
            saveOrUpdateObject(sqlString,comicCheckm);
            return true;
        }
        catch (DataAccessException ex)
        {
            LogHelper.WriteErrorLog(ex);
            return false;
        }
    }

    @Override
    public boolean Delete(ComicCheck comicCheckm) {
        try {
            String sqlString = "update ComicCheck set DeleteMark=1,ModifyTime=:ModifyTime where ID=:ID";
            saveOrUpdateObject(sqlString,comicCheckm);
            return true;
        }
        catch (DataAccessException ex)
        {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            LogHelper.WriteErrorLog(ex);
            return false;
        }
    }


}
